package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.web.dao.WebArticleTypeDao;
import ysq.xyg.web.model.po.WebArticle;
import ysq.xyg.web.model.po.WebArticleType;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebArticleTypeVo;
import ysq.xyg.web.model.vo.WebArticleVo;
import ysq.xyg.web.service.WebArticleService;
import ysq.xyg.web.service.WebArticleTypeService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 业务模块-文章分类service实现
 *
 * @author shengQiang yu
 * @date 2019-4-25 16:49:54
 */
@Service("webArticleTypeService")
public class WebArticleTypeServiceImpl extends BaseServiceImpl<WebArticleType> implements WebArticleTypeService {

    @Resource
    private WebArticleTypeDao webArticleTypeDao;
    @Resource
    private WebArticleService webArticleService;

    private WebArticleType webArticleType;

    @Autowired
    public void setBaseDao(WebArticleTypeDao webArticleTypeDao) {
        this.baseDao = webArticleTypeDao;
    }

    /**
     * 获取下拉框集合
     *
     * @param code code值
     * @author shengQiang yu
     * @date 2019-4-25 16:49:54
     */
    public ReturnEntity acceptSelect(String code) {
        List<WebArticleType> list = acceptList(new WebArticleTypeVo());
        List<SysDictContent> sysDictContentList=new ArrayList<SysDictContent>();
        for(WebArticleType webArticleType:list){
            SysDictContent sysDictContent=new SysDictContent();
            sysDictContent.setId(webArticleType.getId());
            sysDictContent.setName(webArticleType.getName());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(webArticleType.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }
    /**
     * 分页查询
     *
     * @param webArticleTypeVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-25 16:49:54
     */
    public ReturnEntity page(WebArticleTypeVo webArticleTypeVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebArticleType> list = acceptList(webArticleTypeVo);
        PageInfo<WebArticleType> page=new PageInfo<WebArticleType>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webArticleTypeVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-25 16:49:54
     */
    public ReturnEntity addOrUpdate(WebArticleTypeVo webArticleTypeVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webArticleTypeVo.getId())){
            webArticleType=new WebArticleType(currentUser);
            isUpate=false;
        }else{
            webArticleType=selectByPrimaryKey(webArticleTypeVo.getId());
            if(ObjectUtil.isNull(webArticleType)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webArticleType.setUpdateTime(new Date());
            webArticleType.setUpdateUser(currentUser.getId());
        }
        webArticleType.setName(webArticleTypeVo.getName());
        webArticleType.setSort(webArticleTypeVo.getSort());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webArticleType);
        }else{
            num=insert(webArticleType);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-25 16:49:54
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebArticleType webArticleType=selectByPrimaryKey(id);
        List<WebArticle> webArticleList=webArticleService.acceptList(new WebArticleVo(id));
        if(webArticleList!=null&&webArticleList.size()>0){
            return new ReturnEntity(BaseConstant.FAIL,"分类【"+webArticleType.getName()+"】下面还有子类资讯，不能删除！");
        }
        if(webArticleType!=null){
            webArticleType.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webArticleType.setUpdateTime(new Date());
            webArticleType.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webArticleType);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webArticleTypeVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-4-25 16:49:54
     */
    public List<WebArticleType> acceptList(WebArticleTypeVo webArticleTypeVo){
        Example example = new Example(WebArticleType.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webArticleTypeVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webArticleTypeVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return webArticleTypeDao.selectByExample(example);
    }
}
